<!DOCTYPE html>
<html>
	{include file="public/header" /}

	<body>

		<div class="layui-fluid">
			<div class="layui-card">
				<div class="layui-form layui-card-header layuiadmin-card-header-auto">
					<div class="layui-form-item">
						<div class="layui-inline">
							角色筛选
						</div>
						<div class="layui-inline">
							<select name="rolename" lay-filter="LAY-user-adminrole-type">
								{foreach $data as $vo}
								<option value=" {$vo.ID}">{$vo.Name}</option>
								{/foreach}
							</select>
						</div>
					</div>
				</div>
				<div class="layui-card-body">
					<div style="padding-bottom: 10px;">
						<button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
					</div>
					<table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>
					<script type="text/html" id="buttonTpl">
						{{# if(d.check == true){ }}
						<button class="layui-btn layui-btn-xs">已审核</button> {{# } else { }}
						<button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button> {{# } }}
					</script>
					<script type="text/html" id="table-useradmin-admin">
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon"></i>编辑</a>
						<!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
					</script>
				</div>
			</div>
		</div>

		{include file="public/footer" /}
		<script>
			layui.use(['index', 'useradmin', 'table'], function() {
				var $ = layui.$,
					form = layui.form,
					table = layui.table;
				//搜索角色
				form.on('select(LAY-user-adminrole-type)', function(data) {
					//执行重载
					table.reload('LAY-user-back-role', {
						where: {
							role: data.value
						}
					});
				});
				//监听工具条
				table.on('tool(LAY-user-back-role)', function(obj) {
					var data = obj.data;
					if(obj.event === 'detail') {
						layer.msg('ID：' + data.id + ' 的查看操作');
					} else if(obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							$.ajax({
								'type': 'post',
								'url': "{:url('admin/role/del')}",
								'data': {
									'ID': data.ID
								},
								//		              'data':data,
								dataType: 'json',
								success: function(res) {
									if(res.code == '0') {
										layer.msg(res.msg, {
											icon: 1
										});
									} else {
										layer.msg(res.msg, {
											icon: 5
										});
									}
									table.reload('LAY-user-back-role');
									layer.close(index); //关闭弹层
								}
							});

						});
					} else if(obj.event === 'edit') {
						layer.open({
							type: 2,
							title: '编辑角色',
							content: 'editss.html?ID=' + data.ID,
							area: ['80%', '80%'],
							btn: ['确定', '取消'],
							yes: function(index, layero) {
								var iframeWindow = window['layui-layer-iframe' + index],
									submitID = 'LAY-user-role-submit',
									submit = layero.find('iframe').contents().find('#' + submitID);
								//监听提交

								iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
									var field = data.field; //获取提交的字段
									//									console.log(12);
									//提交 Ajax 成功后，静态更新表格中的数据
									$.ajax({
										'type': 'post',
										'url': "{:url('admin/role/edit')}",
										'data': field,
										dataType: 'json',
										success: function(res) {
											if(res.code == '0') {
												layer.msg(res.msg, {
													icon: 1
												});
											} else {
												layer.msg(res.msg, {
													icon: 5
												});
											}
											table.reload('LAY-user-back-role');
											layer.close(index); //关闭弹层
										}
									});

								});

								submit.trigger('click');
							}
						});
					}
				});
				table.render({
					elem: '#LAY-user-back-role',
					url: "{:url('admin/role/get_list')}",
					toolbar: '#LAY-user-back-role',
					title: '用户数据表',
					cols: [
						[{
								type: 'checkbox',
								fixed: 'left'
							}, {
								type: 'numbers',
								title: '排序',
								width: 40,
								fixed: 'left',
								unresize: true,

							}, {
								field: 'Name',
								title: '角色',
								width: 120,
								edit: 'text'
							},

							{
								field: 'MenuID',
								title: '菜单ID',

								edit: 'text',

							},

							{
								field: 'Status',
								title: '状态',

							},

							{
								fixed: 'right',
								title: '操作',
								toolbar: '#table-useradmin-admin',
								width: 80
							}
						]
					],
					page: true
				});
				//事件
				var active = {
					batchdel: function() {
						var checkStatus = table.checkStatus('LAY-user-back-role'),
							checkData = checkStatus.data; //得到选中的数据

						if(checkData.length === 0) {
							return layer.msg('请选择数据');
						}

						layer.confirm('确定124删除吗？', function(index) {

							//提交 Ajax 成功后，静态更新表格中的数据
							$.ajax({
								'type': 'post',
								'url': "{:url('admin/role/add')}",
								'data': field,
								dataType: 'json',
								success: function(res) {
									if(res.code == '0') {
										layer.msg(res.msg, {
											icon: 1
										});
									} else {
										layer.msg(res.msg, {
											icon: 5
										});
									}
									table.reload('LAY-user-back-role');
									layer.close(index); //关闭弹层
								}
							});
							table.reload('LAY-user-back-role');
							layer.msg('已删除');
						});
					},

					add: function() {
						layer.open({
							type: 2,
							title: '添加新角色',
							content: 'roleform.html',
							area: ['80%', '80%'],
							btn: ['确定', '取消'],
							yes: function(index, layero) {
								//								
								var iframeWindow = window['layui-layer-iframe' + index],
									submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

								//监听提交
								iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data) {
									var field = data.field; //获取提交的字段
									//									console.log(56);
									//提交 Ajax 成功后，静态更新表格中的数据
									$.ajax({
										'type': 'post',
										'url': "{:url('admin/role/add')}",
										'data': field,
										dataType: 'json',
										success: function(res) {
											if(res.code == '0') {
												layer.msg(res.msg, {
													icon: 1
												});
											} else {
												layer.msg(res.msg, {
													icon: 5
												});
											}
											table.reload('LAY-user-back-role');
											layer.close(index); //关闭弹层
										}
									});
								});

								submit.trigger('click');
							}
						});
					}
				}
				$('.layui-btn.layuiadmin-btn-role').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>

</html>